//
//  UsedCarTableViewController.m
//  WWXELuShang
//
//  Created by 王涛 on 15/12/17.
//  Copyright © 2015年 lanou. All rights reserved.
//

#import "UsedCarTableViewController.h"
#import "UsedCar.h"
#import "UsedCarTableViewCell.h"
#import "UsedCarDetailViewController.h"
#import "MJRefresh.h"
@interface UsedCarTableViewController ()
@property (nonatomic, strong) NSMutableArray *usedCarArray;
@end

@implementation UsedCarTableViewController

- (NSMutableArray *)usedCarArray {
    if (!_usedCarArray) {
        _usedCarArray = [[NSMutableArray alloc] init];
    }
    return _usedCarArray;
}
static NSInteger i = 1;
- (void)jsonUsedCar {
    NSString *string = [NSString stringWithFormat:@"%ld",i];
    NSString *string1 = [NSString stringWithFormat:kUsedCar,string];
    [LORequestManger GET:string1 success:^(id response) {
        NSDictionary *dic = (NSDictionary *)response;
        NSDictionary *dic1 = dic[@"result"];
        for (NSDictionary *dict in dic1[@"list"]) {
            UsedCar *usedCar = [UsedCar setValuesWithDictionary:dict];
            [self.usedCarArray addObject:usedCar];
        }
        [self.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    i ++;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    i = 1;
    [self jsonUsedCar];
    __weak UsedCarTableViewController *usedCarTVC = self;
    [self.tableView addFooterWithCallback:^{
        [usedCarTVC jsonUsedCar];
        [usedCarTVC.tableView footerEndRefreshing];
    }];
    self.navigationController.navigationBar.barTintColor = kBarColor;
    [self.navigationController.navigationBar setTitleTextAttributes:
     @{NSForegroundColorAttributeName:[UIColor whiteColor]}];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    self.title = @"二手车";
    [self.tableView registerClass:[UsedCarTableViewCell class] forCellReuseIdentifier:@"usedCarCell"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:(UIBarButtonItemStyleDone) target:self action:@selector(back)];
    
}
- (void)back {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.usedCarArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return kHeight / 7;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UsedCarTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"usedCarCell" forIndexPath:indexPath];
    cell.usedCar = self.usedCarArray[indexPath.row];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    UsedCarDetailViewController *usedCarDetailVC = [[UsedCarDetailViewController alloc] init];
    usedCarDetailVC.ID = [self.usedCarArray[indexPath.row] ID];
    //cell选中后去掉背景灰色
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    cell.selectionStyle = UITableViewRowAnimationNone;
    [self.navigationController pushViewController:usedCarDetailVC animated:YES];
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */

/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
 } else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }
 }
 */

/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */

/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
